package com.zkh360.service.message.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zkh360.service.message.constant.ErrorCode;
import com.zkh360.service.message.domain.SysAuthorities;
import com.zkh360.service.message.domain.SysRoles;
import com.zkh360.service.message.domain.SysUsers;
import com.zkh360.service.message.exceptions.BusinessException;
import com.zkh360.service.message.mapper.AuthoritiesMapper;
import com.zkh360.service.message.mapper.RoleMapper;
import com.zkh360.service.message.mapper.UsersMapper;
import com.zkh360.service.message.model.Pager;
import com.zkh360.service.message.model.SysUserVO;
import com.zkh360.service.message.service.UserService;
import com.zkh360.service.message.util.MD5Util;

import lombok.extern.slf4j.Slf4j;

/**
 * 
 * 
 * @author chuanxi.zhao
 *
 */
@Service(value = "UserService")
@Slf4j
public class UserServiceImpl implements UserService {

	@Autowired
	private UsersMapper usersMapper;

	@Autowired
	private AuthoritiesMapper authoritiesMapper;
	
	@Autowired
	private RoleMapper roleMapper;

	@Override
	public List<SysUserVO> getUsers(SysUserVO sysUserVo) throws BusinessException {
		log.info("");
		SysUsers sysUsersParams = new SysUsers();
		sysUsersParams.setUser_id(sysUserVo.getUser_id());
		sysUsersParams.setUser_account(sysUserVo.getUser_account());
		List<SysUsers> sysUsers = usersMapper.select(sysUsersParams);
		List<SysUserVO> sysUserVos = new ArrayList<>();
		sysUsers.stream().forEach(sysUser -> {
			SysUserVO sysUserVoTemp = new SysUserVO();
			BeanUtils.copyProperties(sysUser, sysUserVoTemp);
			sysUserVos.add(sysUserVoTemp);
		});
		return sysUserVos;
	}

	@Override
	public List<SysUserVO> getUsers(SysUserVO sysUserVo, Pager pager) throws BusinessException {
		SysUsers sysUsersParams = new SysUsers();
		sysUsersParams.setUser_account(sysUserVo.getUser_account());
		SysUsers userFromDb = usersMapper.selectOne(sysUsersParams);
		//  先查用户角色  在根据用户权限查用户
		List<SysRoles> sysRolesList = roleMapper.getRolesByUser(userFromDb.getUser_id(), userFromDb.getUser_account(), 0, 1000);
		//
		List<Long> roleIdList = new ArrayList<>();
		sysRolesList.forEach(sysRole -> { roleIdList.add(sysRole.getRole_id()); });
		//
		List<SysUserVO> sysUserVos = usersMapper.getUserByRoleId(roleIdList, null);
		return sysUserVos;
	}

	@Override
	public void addUser(SysUserVO sysUserVo) {
		// 防止用户名重复，因为要用来登录
		SysUsers sysUsersParams = new SysUsers();
		sysUsersParams.setUser_account(sysUserVo.getUser_account());
		SysUsers sysUsersFromDb = usersMapper.selectOne(sysUsersParams);
		if (sysUsersFromDb == null) {
			SysUsers sysUsers = new SysUsers();
			sysUsers.setEnabled(sysUserVo.getEnabled());
			sysUsers.setIssys(sysUserVo.getIssys());
			sysUsers.setUser_account(sysUserVo.getUser_account());
			sysUsers.setUser_desc(sysUserVo.getUser_desc());
			sysUsers.setUser_name(sysUserVo.getUser_name());
			sysUsers.setUser_password(MD5Util.encode(sysUserVo.getUser_password()));
			usersMapper.insert(sysUsers);
		} else {
			throw BusinessException.customException(ErrorCode.CODE_FAILURE, "用户登录名称不允许重复！");
		}
	}

	@Override
	public void deleteUser(Long userId) {
		// 需要判断所有的角色都没有配置该权限才成删除
		List<SysAuthorities> sysAuthoritiesList = authoritiesMapper.getAuthoritiesByUser(userId, null);
		if (sysAuthoritiesList.size() > 0) {
			throw BusinessException.customException(ErrorCode.CODE_FAILURE, "该用户配置许多角色，无法删除！");
		} else {
			SysUsers sysUsersParams = new SysUsers();
			sysUsersParams.setUser_id(userId);
			SysUsers sysUsersFromDb = usersMapper.selectOne(sysUsersParams);
			sysUsersFromDb.setEnabled(0);
			usersMapper.updateByPrimaryKey(sysUsersFromDb);
		}
	}

	@Override
	public void editUser(SysUserVO sysUserVo) {
		SysUsers sysUsersParams = new SysUsers();
		sysUsersParams.setUser_id(sysUserVo.getUser_id());
		SysUsers sysUsersFromDb = usersMapper.selectOne(sysUsersParams);
		if (sysUsersFromDb == null) {
			throw BusinessException.customException(ErrorCode.CODE_FAILURE, "用户登录名称不允许重复！");
		} else {
			sysUsersFromDb.setEnabled(sysUserVo.getEnabled());
			sysUsersFromDb.setIssys(sysUserVo.getIssys());
			sysUsersFromDb.setUser_account(sysUserVo.getUser_account());
			sysUsersFromDb.setUser_desc(sysUserVo.getUser_desc());
			sysUsersFromDb.setUser_name(sysUserVo.getUser_name());
			if(sysUsersFromDb.getUser_password().equals(sysUserVo.getUser_password())) {
				usersMapper.updateByPrimaryKey(sysUsersFromDb);
			}else {
				sysUsersFromDb.setUser_password(MD5Util.encode(sysUserVo.getUser_password()));
				usersMapper.updateByPrimaryKey(sysUsersFromDb);
			}
		}
	}

	@Override
	public int getUsersCount(SysUserVO sysUsers) {

		SysUsers sysUsersParams = new SysUsers();
		sysUsersParams.setUser_account(sysUsers.getUser_account());
		SysUsers userFromDb = usersMapper.selectOne(sysUsersParams);
		//  先查用户角色  在根据用户权限查用户
		List<SysRoles> sysRolesList = roleMapper.getRolesByUser(userFromDb.getUser_id(), userFromDb.getUser_account(), 0, 1000);
		//
		List<Long> roleIdList = new ArrayList<>();
		sysRolesList.forEach(sysRole -> { roleIdList.add(sysRole.getRole_id()); });
		return usersMapper.getUsersCount(roleIdList, new SysUserVO());
	}

}
